<?php
/**
 * Created by : Sandi Rizky Kharisma (sandi.informatics08@gmail.com)
 * This Class Generated for CRUD purpose only
 */
class m_toko extends CI_Model
{

    function __construct()
    {
        parent::__construct();
    }

    public function get($where = null, $limit = null, $offset = null)
    {
        if($where != null) $this->db->where($where);
        if($limit != null && $offset != null) $this->db->limit($limit, $offset);
        return $this->db->get("toko");
    }

    function get_all($limit, $uri)
    {
        $result = $this->db->get('toko', $limit, $uri);
        if ($result->num_rows() > 0)
        {
            return $result->result_array();
        } else
        {
            return array();
        }
    }

    function get_one($id)
    {
        $this->db->where('ID_TOKO', $id);
        $result = $this->db->get('toko');
        if ($result->num_rows() == 1)
        {
            return $result;
        } else
        {
            return array();
        }
    }

    function drop_table($id)
    {
        $s = "DROP TABLE `barang_$id`";
        $this->db->query($s);
        $s = "DROP TABLE `barang_toko_$id`";
        $this->db->query($s);
        $s = "DROP TABLE `faktur_beli_$id`";
        $this->db->query($s);
        $s = "DROP TABLE `order_beli_$id`";
        $this->db->query($s);
        $s = "DROP TABLE `order_faktur_$id`";
        $this->db->query($s);
    }

    function create_table($id)
    {
        $s = "
        CREATE TABLE IF NOT EXISTS `barang_$id` (
            `KODE_BARANG` int(11) NOT NULL AUTO_INCREMENT,
            `ID_JENIS_BARANG` int(11) DEFAULT NULL,
            `PEMILIK_BARANG` VARCHAR(30) NOT NULL,
            `NAMA_BARANG` varchar(70) NOT NULL,
            `MERK` varchar(70) DEFAULT NULL,
            `NAMA_SATUAN` varchar(3) NOT NULL,
            `MODE_TOKO` int(11) NOT NULL,
            PRIMARY KEY (`KODE_BARANG`),
            KEY `FK_RELATIONSHIP_7` (`ID_JENIS_BARANG`)
            ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
        ";

        $this->db->query($s);

        $s = "CREATE TABLE IF NOT EXISTS `barang_toko_$id` (
            `KODE_BARANG` int(11) DEFAULT NULL,
            `HARGA_BELI` float(11,0) NOT NULL,
            `HARGA_JUAL` float(11,0) NOT NULL,
            `JUMLAH_AWAL` int(11) NOT NULL,
            `STOK_MIN` int(11) NOT NULL,
            `JUMLAH_AKHIR` int(11) NOT NULL,
            `DISKON` decimal(11,0) NOT NULL,
            `TGL_ENTRY` datetime NOT NULL,
            KEY `FK_RELATIONSHIP_5` (`KODE_BARANG`)
            ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
        ";

        $this->db->query($s);

        $s = "CREATE TABLE IF NOT EXISTS `faktur_beli_$id` (
            `NOMER_FAKTUR` int(11) NOT NULL,
            `TGL_FAKTUR` datetime DEFAULT NULL,
            PRIMARY KEY (`NOMER_FAKTUR`)
            ) ENGINE=MyISAM DEFAULT CHARSET=latin1;";

        $this->db->query($s);

        $s = "CREATE TABLE IF NOT EXISTS `order_beli_$id` (
            `NOMER_ORDER` int(11) NOT NULL,
            `ID_SUPPLIER` int(11) DEFAULT NULL,
            `NAMA_SUPPLIER` varchar(64) DEFAULT NULL,
            `TGL_ORDER` datetime DEFAULT NULL,
            PRIMARY KEY (`NOMER_ORDER`)
            ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
        ";

        $this->db->query($s);

        $s = "CREATE TABLE IF NOT EXISTS `order_faktur_$id` (
            `NOMER_FAKTUR` int(11) DEFAULT NULL,
            `NOMER_ORDER` int(11) DEFAULT NULL,
            `KODE_BARANG` int(11) DEFAULT NULL,
            `NAMA_BARANG` varchar(64) DEFAULT NULL,
            `SATUAN` varchar(3) DEFAULT NULL,
            `JENIS` int(11) DEFAULT NULL,
            `JUMLAH` int(11) DEFAULT NULL,
            `TOTAL_HARGA` int(11) DEFAULT NULL
            ) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
        $this->db->query($s);
    }

    function insert()
    {
        $data = array(

                'NAMA_TOKO' => $this->input->post('NAMA_TOKO', TRUE),

                'ALAMAT' => $this->input->post('ALAMAT', TRUE),

                'TELPON' => $this->input->post('TELPON', TRUE),

                'NAMA_KOTA' => $this->input->post('NAMA_KOTA', TRUE),

                'FACIMILE' => $this->input->post('FACIMILE', TRUE),

                'PIMPINAN' => $this->input->post('PIMPINAN', TRUE),

                'KASIR' => $this->input->post('KASIR', TRUE),

                'PTG_PEMBELIAN' => $this->input->post('PTG_PEMBELIAN', TRUE),

                'PTG_TERIMA_BARANG' => $this->input->post('PTG_TERIMA_BARANG', TRUE),

        );
        $result = $this->db->insert('toko', $data);

        $id = $this->db->insert_id();

        $this->create_table($id);

        return $result;
    }

    function update($id)
    {
        $data = array(
            
                'NAMA_TOKO' => $this->input->post('NAMA_TOKO', TRUE),

                'ALAMAT' => $this->input->post('ALAMAT', TRUE),

                'TELPON' => $this->input->post('TELPON', TRUE),

                'NAMA_KOTA' => $this->input->post('NAMA_KOTA', TRUE),

                'FACIMILE' => $this->input->post('FACIMILE', TRUE),

                'PIMPINAN' => $this->input->post('PIMPINAN', TRUE),

                'KASIR' => $this->input->post('KASIR', TRUE),

                'PTG_PEMBELIAN' => $this->input->post('PTG_PEMBELIAN', TRUE),

                'PTG_TERIMA_BARANG' => $this->input->post('PTG_TERIMA_BARANG', TRUE),

        );
        $this->db->where('ID_TOKO', $id);
        return $this->db->update('toko', $data);
    }

    function delete($id)
    {
        $this->db->where('ID_TOKO', $id);
        $this->drop_table($id);
        return $this->db->delete('toko');
    }

}
?>
